%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%
clear all; close all; clc;

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Modify main_str to match path on your machine
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
main_str = 'C:\Users\Marco\Dropbox\Housing_Brainstorming\Information_disclosure';
% main_str = 'L:\agargano\Dropbox\Housing_Brainstorming\Information_disclosure';

load_str = [main_str,'\Replication_code_RFS\Model\Output\Last'];

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%% Import 

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Load auction outcomes given number of bidders and alpha
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
load([load_str,'\equilibrium_step1_last.mat']);

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%% Solve Model

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
q_vec = start_bid_scale-1; % vector of values of alpha

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Calibrate number of bidders
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
max_num           = 12; % maximum number of bidders
min_num           = 3;  % minimum number of bidders  
tot_num           = max_num - min_num + 1; % range of matched bidders

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
P_vec        = (0:0.01:1)'; % vector of values of the probability of determining private value (q)
n_p          = size(P_vec,1);

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
n_new_sim    = 5000000; % number of draws from mixing probability in simulation

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
C            = 5000; % paramter $C$ for info acq cost function
J            = 2; % solve 2 models: standard info acq cost function and info cost of AUD 8,000

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Allocate Memory
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
eq_EbidW_mat     = nan(n_start,J);
eq_EbidN_mat     = nan(n_start,J);
eq_Erev_mat      = nan(n_start,J);
eq_Esales_mat    = nan(n_start,J);
eq_Srev_mat      = nan(n_start,J);
eq_Ssales_mat    = nan(n_start,J);
eq_Psales_mat    = nan(n_start,J);
eq_NoWithd_mat   = nan(n_start,J);
eq_Nall_mat      = nan(n_start,J);
eq_p_hcost0_mat  = nan(n_start,J);
eq_EbidNAct_mat  = nan(n_start,J);
eq_EbidWAct_mat  = nan(n_start,J);

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
for j = 1:J % loop over different choice of info acq cost
    
    %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
    poolobj         = parpool('local');
   
    %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
    parfor k = 1:n_start % parallel loop over values of alpha

        %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
        P_vec_sel        = P_vec; % entry probabilities vector
        if j == 1
            CC = abs(q_vec(k))*C; % standard info acq cost function 
        elseif j == 2
            CC = 8000; % info acq cost is AUD 8,000
        end 

        %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
        % Allocate memory
        %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%        
        eq_Nall_curr     = nan(n_p,1);
        eq_EbidW_curr    = nan(n_p,1);
        eq_EbidN_curr    = nan(n_p,1);
        eq_Erev_curr     = nan(n_p,1);
        eq_Esales_curr   = nan(n_p,1);
        eq_Psales_curr   = nan(n_p,1);
        eq_NoWithd_curr  = nan(n_p,1);     

        %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%        
        for i = 1:n_p % loop over values of probability of determining private value

            %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
            for nn = min_num:max_num % loop over number of bidders

    
                %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%   
                % Allocate memory
                %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
                EbidderWns_curr    = nan(n_new_sim,1);
                EbidderNns_curr    = nan(n_new_sim,1);        
                Erev_curr          = nan(n_new_sim,1); 
                Esales_curr        = nan(n_new_sim,1);
                Psales_curr        = nan(n_new_sim,1);
                NoWithd_curr       = nan(n_new_sim,1);
                num_no_hcost_curr  = nan(n_new_sim,1);          

                %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%            
                %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
                % Simulate Decisions To Determine Private Values, and Calculate Auction Outcomes
                %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
        
                for n = 1:n_new_sim
                
                    %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%                
                    % Decisions
                    %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%                                    
                    ind_h0 = rand(nn  ,1)<=P_vec_sel(i,1); % bidders determining private value, unconditional
                    ind_hB = rand(nn-1,1)<=P_vec_sel(i,1); % bidders determining private value, conditional on bidder i entering
                    nmax   = sum(ind_h0);   % number of bidders determining private value, unconditional
                    nmaxB  = sum(ind_hB)+1; % number of bidders determining private value, conditional on bidder i entering

                    %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
                    if nmax > 1 % if there is more than one bidder determining private value
                        Erev_curr(n,1)         = Erev_mat(nmax,k); % seller revenue
                        Esales_curr(n,1)       = Esales_mat(nmax,k); % expected sales price - seller reservation
                        Psales_curr(n,1)       = Psales_mat(nmax,k); % probability of sale
                        num_no_hcost_curr(n,1) = nmax; % number of bidders determining private value
                        NoWithd_curr(n,1)      = 1; % auction is not called off                   
                        EbidderNns_curr(n,1)   = Ebidders_mat(nmax,k); % share of bidders who enter that join auction (b>entry bid)                       
                    end 

                    %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%                
                    if nmax == 1 % if there is one bidder determining private value (auction is called off)
                        EbidderNns_curr(n,1)   = nan;                
                        Erev_curr(n,1)         = 0;                
                        Esales_curr(n,1)       = nan;
                        Psales_curr(n,1)       = 0;
                        num_no_hcost_curr(n,1) = 1;                    
                        NoWithd_curr(n,1)      = 0;
                    end

                    %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%                
                    if nmax == 0 % if there is no bidder determining private value (auction is called off)
                        EbidderNns_curr(n,1)   = nan;                
                        Erev_curr(n,1)         = 0;                
                        Esales_curr(n,1)       = nan;
                        Psales_curr(n,1)       = 0;
                        num_no_hcost_curr(n,1) = 0;                    
                        NoWithd_curr(n,1)      = 0;
                    end                

                    %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%                  
                    if nmaxB > 1 % expected surplus for bidder i conditional on determining value, net of cost CC
                        EbidderWns_curr(n,1)   = EbidderW_mat(nmaxB,k) - CC;    
                    elseif  nmaxB == 1 % if bidder i is the only one determining value, she pays the info acq cost, but there is no auction
                        EbidderWns_curr(n,1)   = - CC; 
                    end    


                end

                %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
                % Average Outcomes Across Different Numbers of Matched Bidders
                %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
                if nn == min_num
                    eq_Nall_curr(i)     = nanmean(num_no_hcost_curr)/tot_num; 
                    eq_EbidW_curr(i)    = nanmean(EbidderWns_curr)*P_vec_sel(i)/tot_num; % expected surplus for bidder i, depends on mixing probability of determining private value
                    eq_EbidN_curr(i)    = nanmean(EbidderNns_curr)/tot_num;         
                    eq_Erev_curr(i)     = nanmean(Erev_curr)/tot_num; 
                    eq_Esales_curr(i)   = nanmean(Esales_curr)/tot_num;
                    eq_Psales_curr(i)   = nanmean(Psales_curr)/tot_num;
                    eq_NoWithd_curr(i)  = nanmean(NoWithd_curr)/tot_num;                    
                else
                    eq_Nall_curr(i)     = eq_Nall_curr(i)    + (nanmean(num_no_hcost_curr)/tot_num);
                    eq_EbidW_curr(i)    = eq_EbidW_curr(i)   + (nanmean(EbidderWns_curr)*P_vec_sel(i)/tot_num);
                    eq_EbidN_curr(i)    = eq_EbidN_curr(i)   + (nanmean(EbidderNns_curr)/tot_num);            
                    eq_Erev_curr(i)     = eq_Erev_curr(i)    + (nanmean(Erev_curr)/tot_num);
                    eq_Esales_curr(i)   = eq_Esales_curr(i)  + (nanmean(Esales_curr)/tot_num);
                    eq_Psales_curr(i)   = eq_Psales_curr(i)  + (nanmean(Psales_curr)/tot_num);
                    eq_NoWithd_curr(i)  = eq_NoWithd_curr(i) + (nanmean(NoWithd_curr)/tot_num);                    
                end     
                %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
        
            end
        end

        %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
        [~,find_max]         = max(eq_EbidW_curr); % bidder i selects, given alpha, the entry probability that maximizes her surplus

        %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%    
        eq_EbidW_mat(k,j)    = eq_EbidW_curr(find_max); % expected surplus
        eq_EbidN_mat(k,j)    = eq_EbidN_curr(find_max); % expected share of bidders joining the auction
        eq_Erev_mat(k,j)     = eq_Erev_curr(find_max); % expected seller revenue
        eq_Esales_mat(k,j)   = eq_Esales_curr(find_max); % expected sales price - seller reservation
        eq_Psales_mat(k,j)   = eq_Psales_curr(find_max); % sales probability
        eq_NoWithd_mat(k,j)  = eq_NoWithd_curr(find_max); % expected number of bidders determining private value
        eq_p_hcost0_mat(k,j) = P_vec_sel(find_max); % probability of determining private value
    end

    %%%%%%%%%%%%%%%%%%%%
    delete(poolobj);

    %%%%%%%%%%%%%%%%%%%%
    j

end

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%% Save

save([load_str,'\Results_out_1_Nash_Loop.mat']);
